package com.assistant.hanleTask;

import com.assistant.utils.FileHandle;

import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import static com.assistant.utils.CommonDefault.getPlatformSeparator;

/**
 * 一键替换 sdk 的 so 文件
 */
public class ReplaceSdkVersion implements Runnable {
    private JTextField apkPath;
    private JTextField sdkPath;
    private JProgressBar jProgressBar;
    private JPanel mjPanel;

    public ReplaceSdkVersion(JTextField apkPath, JTextField sdkPath, JProgressBar jProgressBar, JPanel mjPanel) {
        this.apkPath = apkPath;
        this.sdkPath = sdkPath;
        this.jProgressBar = jProgressBar;
        this.mjPanel = mjPanel;
    }

    @Override
    public void run() {
        String apkPathV = getTextFieldPath(apkPath);
        String sdkPathV = getTextFieldPath(sdkPath);
        if (apkPathV.isEmpty() && sdkPathV.isEmpty()) {
            HandleCommand.handleTextFieldIsEmpty(mjPanel, "源路径和 sdk 路径均不能为空！！");
        }
        //准备工作
        //.apk
        File filename = new File(apkPathV);
        //目录
        String directory = apkPathV.substring(0, apkPathV.lastIndexOf(getPlatformSeparator()));
        //日志文件
        File logFile = new File(directory + getPlatformSeparator() + "log.txt");
        File apktool = new File("apktool.jar");
        File signedJKS = new File("feihu1.jks");
        HandleCommand.logFileIsExist(logFile);

        FileOutputStream logWriter = null;
        try {
            logWriter = new FileOutputStream(logFile);
            if (!apktool.exists()) {
                logWriter.write("can't find apktool.jar,please put it in the peer directory \n".getBytes());
                return;
            }
            if (!signedJKS.exists()) {
                logWriter.write("can't find the Signature file，please put it in the peer directory\n".getBytes());
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //反编译apk文件夹
        String decodeTargetPath = apkPathV.substring(0, apkPathV.lastIndexOf("."));
        String cmdDecode = "java -jar apktool.jar d " + apkPathV + " -o " + decodeTargetPath;
        //需要处理
        String cmdCompile = "java -jar apktool.jar b " + decodeTargetPath;
        //需要处理
        String cmdSigned = "jarsigner -verbose -keystore feihu1.jks " +
                "-signedjar " + decodeTargetPath + getPlatformSeparator() + "dist" + getPlatformSeparator() + "game_signed.apk -storepass 123qwe " + decodeTargetPath + getPlatformSeparator() + "dist" + getPlatformSeparator() + filename.getName() + " key0";
        String content = null;
        //处理工作都完成了
        HandleCommand.setjProgressValue(jProgressBar, 10);
        //反编译
        try {
            logWriter.write("Preparatory work completed\n".getBytes());

            //反编译
            content = HandleCommand.handleCommandForMakeLogApk(cmdDecode, logWriter);
            HandleCommand.setjProgressValue(jProgressBar, 30);
            //替换 so 文件
            replaceSDK(decodeTargetPath, sdkPathV,logWriter);
            //打一调试版本的包
            HandleCommand.parseXml(decodeTargetPath);
            //反打包
            content += HandleCommand.handleCommandForMakeLogApk(cmdCompile, logWriter);
            HandleCommand.setjProgressValue(jProgressBar, 70);
            logWriter.write("start signing... \n".getBytes());
            //签名
            content += HandleCommand.handleCommandForMakeLogApk(cmdSigned, logWriter);
            HandleCommand.setjProgressValue(jProgressBar, 90);
            logWriter.write(content.getBytes());
            logWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        HandleCommand.setjProgressValue(jProgressBar,100);
    }

    private void replaceSDK(String apkPath, String sdkPath,FileOutputStream logFile) {
        FileHandle.Builder builder=FileHandle.Builder.getInstance(logFile);
        FileHandle fileHandle=builder.setSourcePath(apkPath).setTargetPath(sdkPath).setFilename("libQsu.so").Build();
        fileHandle.replaceAll();
    }

    private String getTextFieldPath(JTextField jTextField) {
        return jTextField.getText().trim();
    }

}
